﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using NhaThuocOnline.Entities;
using NhaThuocOnline.Repositories;
using NhaThuocOnline.ViewModels;

namespace NhaThuocOnline.Service
{
    public class PhanLoaiThuocService
    {
        public void InsertOrUpdate(ViewModelsPhanLoaiThuoc plt)
        {
            var en = Mapper.Map<ViewModelsPhanLoaiThuoc, PhanLoaiThuoc>(plt);
            using (PhanLoaiThuocRepository repo = new PhanLoaiThuocRepository())
            {
                repo.InsertOrUpdate(en);
                repo.Save();
            }
        }
        public void Delete(int id)
        {
            using (PhanLoaiThuocRepository repo = new PhanLoaiThuocRepository())
            {
                repo.Delete(id);
                repo.Save();
            }
        }
        public List<ViewModelsPhanLoaiThuoc> GetAll()
        {
            using (PhanLoaiThuocRepository repo = new PhanLoaiThuocRepository())
            {
                return Mapper.Map<List<PhanLoaiThuoc>, List<ViewModelsPhanLoaiThuoc>>(repo.GetAll.ToList());
            }
        }
        public ViewModelsPhanLoaiThuoc GetPhanLoaiThuocById(int id)
        {
            using (PhanLoaiThuocRepository repo = new PhanLoaiThuocRepository())
            {
                return Mapper.Map<PhanLoaiThuoc, ViewModelsPhanLoaiThuoc>(repo.GetAll.Where(bn => bn.Id == id).FirstOrDefault());
            }
        }
    }
}
